(function(window){

    var require = function(id, pre){
        this.packageWarehouse = '267_modules';
        this.id = id;
        this.pre = pre || "";
        this.init();
    };

    require.prototype = {
        init        : function(){
            this.analysisID();
            this.fileName();
        },
        analysisID  : function(){
            var idArray = this.id.split('/');
            var finalID = '';
            if(this.pre){
                this.pre = this.pre.split('/');
                this.pre.pop();
                this.pre.join('/');
            };
            if(idArray[0] === ''){
                finalID = this.id;
            }else if(idArray[0] === '.'){
                idArray.shift();
                this.id = '/'+idArray.join('/');
                finalID = this.pre + this.id;
            }else if(idArray[0] === '..'){
                this.id = '/'+idArray.join('/');
                finalID = this.pre + this.id;
            }else{
                for(var i in package){
                    if(idArray[0] === i){
                        idArray[0] = this.packageWarehouse +'/' + package[i].path;
                        break;
                    };
                };
                this.id = '/'+idArray.join('/');
                finalID = this.pre + this.id;
            };

            finalID = finalID.substr(1);

            this.final = finalID;
            //util.ajax({
            //    url: finalID,
            //    param : {finalID: finalID},
            //    success: function(code, param){
            //        warehouse[param.finalID] = code;
            //    }
            //});
        },
        fileName    : function(){
            var finalArray  = this.final.split('/')
            ,   length      = finalArray.length
            ,   ending      = finalArray[length - 1]
            ,   endingArray = ending.split('.')
            ,   fileName;

            if(endingArray.length === 2 && endingArray[1] === 'js'){
                fileName = [endingArray.join('.')];
            }else{
                fileName = [endingArray.join('.')+'.js', endingArray.join('.')+'/index.js'];
            };

            console.log(fileName);

        }
    };

    var _require = window.require = function(id, pre){
        return new require(id, pre);
    };

    var util = {
        ajax: function(options){
            var _xhr = new XMLHttpRequest();
            _xhr.open("GET", options.url, options.async || true);
            try {
                _xhr.send(null);
            }catch (err){
                options.error && options.error(options.data);
            };

            _xhr.onreadystatechange = function () {
                if (_xhr.readyState == 4) {
                    var status = _xhr.status;
                    if (status == 200 || status == 0 && _xhr.response) {
                        options.success && options.success(_xhr.response, options.param);
                    } else {
                        options.error && options.error();
                    }
                    _xhr = null;
                };
            };
        }
    };

    var warehouse = {};
    var package = {};
    var startAPP = document.getElementsByTagName('script');

    for(var i = 0; i < startAPP.length; i++){
        if(startAPP[i].getAttribute('267-main')){
            startAPP = startAPP[i].getAttribute('267-main');
            break;
        };
    };

    util.ajax({
        url : 'package.json',
        async: false,
        success : function(data){
            package = JSON.parse(data).dependencies;
            _require(startAPP, '');
        }
    });

    window.exports = {};
    window.module = {
        exports: window.exports
    };
})(window);